package controller;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import dao.StudentDao;
import entity.Student;

public class Mainoracle extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField[] tf=new JTextField[3];
	private JButton[] bt=new JButton[3];
	private StudentDao sd;
	
	public Mainoracle() {
		setTitle("学生信息");
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(null);
		setSize(500, 500);
		setLocationRelativeTo(null);
				
		String[] lab={"id","name","sex"};
		String[] jbt={"添加信息","修改信息","删除信息"};
		Font font=new Font("楷体", Font.BOLD, 20);
		for(int i=0;i<lab.length;i++){
			JLabel s=new JLabel(lab[i]);
			s.setBounds(80, 60+i*60, 100, 30);
			s.setFont(font);
			add(s);
					
			
			tf[i]=new JTextField();
			tf[i].setBounds(150, 60+i*60, 200, 30);
			add(tf[i]);
			
			bt[i]=new JButton(jbt[i]);
			bt[i].setFont(font);
			bt[i].setBounds(150, 230+60*i, 200, 30);
			bt[i].addActionListener(this);
			add(bt[i]);
			
			JButton bf=new JButton("查询信息");
			bf.setFont(font);
			bf.setBounds(150, 410, 200, 30);
			bf.addActionListener(this);
			add(bf);
		};	
		
	    
	    sd=new StudentDao();
	    
	}	
	public static void main(String[] args) {
		
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new Mainoracle();				
			}
		});
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(tf[0].getText().equals("")||tf[0].getText().startsWith(" "))
		{
			JOptionPane.showMessageDialog(null, "id不能为空");
			return;
			}	   
	    switch(e.getActionCommand())
	    {
	     case "添加信息": Insert();break;
		
	     case "修改信息":update();break;
	    	    
	     case "删除信息":delete();break;
	     
	     
	    }		
	}
	private void delete() {
		// TODO Auto-generated method stub
		
	}
	private void update() {
		// TODO Auto-generated method stub
		
	}
	private void Insert() {
		Student s=new Student();
		s.setId(Integer.valueOf(tf[0].getText()));
		s.setName(tf[1].getText());
		s.setSex(tf[2].getText());
		sd.insert(s);
	}		
}
